/*
 * @copyright: Huang Ding
 * @Author: ding-cx
 * @Date: 2020-12-12 15:05:38
 * @LastEditors: ding-cx
 * @LastEditTime: 2020-12-12 18:02:41
 * @Description: file content
 */
const Controller = require('egg').Controller

class UserController extends Controller {
    // 待删除
    async getList() {
        const {
            app, ctx
        } = this;
        const db = app.mysql.get("devdb");
        // const list = await db.select("sys_user",{
        //     // where:{"create_time":"2020-12-12 15:51:25"}
        // });
        console.log(app.config.ding_config);
        app.config.ding_config = {
            he: "hehehe update"
        }


        const list = await db.query("select DATE_FORMAT(sys_user.create_time,'%Y-%m-%d %H:%i:%s') AS create_time from sys_user where sys_user.create_time > ?", ['2020-12-12 15:51:00'])
        //  ctx.send({
        //     list,
        //     config:this.config.ding_config 
        //  })


        ctx.echo({ list }, 50000)
        ctx.echo({ config: this.config.ding_config })
        console.log(this.ctx.body);
    }



    async login() {
        const { app, ctx } = this;
        // TODO 验证验证码是否匹配


        // 匹配用户输入和数据库的账户密码是否匹配。
        const dbUser = await app.db.select("sys_user", {
            where: { user_name: ctx.request.body.user_name, password: ctx.request.body.password }
        })
        console.log(dbUser);

        // 如果用户密码未匹配到数据库。
        if(!dbUser){
            ctx.send("用户名或密码错误！",50000);
            return;
        }
        // 验证通过。
        // 生成jwt token
        const token = app.jwt.sign(dbUser, this.config.jwt.secret, {
            expiresIn: this.config.jwt.expires
        })
        console.log(token);
        console.log(ctx.request.body);
        ctx.send({ token })
    }
}

module.exports = UserController;